package com.chx.rpc01;


import com.chx.dto.User;
import com.chx.rpc01.UserServiceImpl;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {


    private static boolean running=true;

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while(running) {
            Socket socket = serverSocket.accept();
            process(socket);
            socket.close();
        }
        serverSocket.close();
    }

    public static void process(Socket socket) throws IOException {
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        DataInputStream dis=new DataInputStream(is);
        DataOutputStream dos = new DataOutputStream(os);

        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.findUserById(dis.readInt());

        dos.writeInt(user.getId());
        dos.writeUTF(user.getUserName());
        dos.flush();

        dis.close();
        dos.close();
        System.out.println(is.markSupported());
    }
}
